package com.pet.system.controller;

import com.github.pagehelper.PageInfo;
import com.pet.system.common.AjaxResult;
import com.pet.system.common.BaseController;
import com.pet.system.controller.vo.OrderVo;
import com.pet.system.entity.Order;
import com.pet.system.entity.User;
import com.pet.system.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/order")
@Api(value = "订单")
public class OrderController extends BaseController {
    @Autowired
    private OrderService orderService;

    @GetMapping("/list")
    @ApiOperation("分页")
    public AjaxResult list(OrderVo order) {
        PageInfo<Order> result = orderService.getPageList(order);

        return AjaxResult.success(result);
    }
    @GetMapping("/userlist")
    @ApiOperation("客户分页")
    public AjaxResult userlist(OrderVo order) {
        User user = getUser();
        order.setAccounts(user.getNickname());
        PageInfo<Order> result = orderService.getPageList(order);

        return AjaxResult.success(result);
    }
    @GetMapping("/orderNum")
    @ApiOperation("七天内订单数")
    public AjaxResult orderNum( ) {
        List<Map<String,Integer>> result = orderService.orderNum();

        return AjaxResult.success(result);
    }

    @GetMapping("/staffOrder")
    @ApiOperation("员工订单")
    public AjaxResult staffOrder( ) {
        List<Map<String,Integer>> result = orderService.staffOrder();

        return AjaxResult.success(result);
    }

    @GetMapping(value = {"/{id}"})
    @ApiOperation("详情")
    public AjaxResult getInfo(@PathVariable(value = "id", required = false) Long id) {
        Order order= orderService.getInfo(id);
        return AjaxResult.success(order);
    }

    @PostMapping
    @ApiOperation("添加")
    public AjaxResult add(@Validated @RequestBody Order order) {

        User user = getUser();
        order.setAccounts(user.getNickname());

        return toAjax(orderService.add(order));
    }

    @PutMapping
    @ApiOperation("修改")
    public AjaxResult edit(@Validated @RequestBody Order order) {
        User user = getUser();
        order.setStaffId(user.getNickname());
        return toAjax(orderService.edit(order));
    }

    @DeleteMapping("/{ids}")
    @ApiOperation("批量删除")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderService.remove(ids));
    }
}
